package control;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.taglibs.standard.tag.common.core.OutSupport;
import org.apache.tomcat.util.http.fileupload.MultipartStream;

import com.oreilly.servlet.MultipartRequest;
import com.sun.xml.internal.messaging.saaj.soap.MultipartDataContentHandler;

import dao.UserData;

/**
 * Servlet implementation class SaveCommunication
 */
public class SaveCommunication extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveCommunication() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String email = (String) session.getAttribute("email");
		UserData us = new UserData();
		MultipartRequest mtr = new MultipartRequest(request, getServletContext().getRealPath("post"),5*1024*1024,"UTF-8");
		String content = mtr.getParameter("content");
		String title = mtr.getParameter("title");
		String type = mtr.getParameter("type");
		int itype = Integer.parseInt(type);
		long timeContent = System.currentTimeMillis();
		String link = "";
		String content_err = "";
		String title_err ="";
		String meta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
		String jsp = "<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\"pageEncoding=\"UTF-8\"%>";
		if(content.equals("")){
			content_err = " Bạn chưa nhập Nội dung ";
		}
		if(title.equals("")){
			title_err = " Bạn chưa nhập tiêu đề ";
		}
		if(content_err.equals("")&& title_err.equals("")){
			try {
				String temp = getServletContext().getRealPath("post");
				FileOutputStream fos = new FileOutputStream(temp+"/"+timeContent+".jsp",true);
//				OutputStreamWriter osw = new OutputStreamWriter(fos);
				PrintWriter pw = new PrintWriter(fos, true);
				pw.write(jsp);
				pw.write(meta);
 				pw.write(content);
				link = "post/"+timeContent+".jsp";
				us.addCommunication(link,timeContent,title,itype,email);
				pw.close();
			} catch (Exception e) {
				response.sendRedirect("textEditor.jsp");
			}
		}
		
		String url = "/index.jsp";
		if(content_err.length()>0|| title_err.length()>0){
			url = "/textEditor.jsp";
			request.setAttribute("content_err", content_err);
			request.setAttribute("title_err", title_err);
		}
		
		RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
